package org.neuedu.JWT;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.neuedu.entity.Employee;

import java.util.Date;

public class JwtUtil {
    private static final String KEY = "LIZHANWEN";
    public static String createToken (Employee employee) {
        return JWT.create().withClaim("id", employee.getId())
                .withClaim("phone", employee.getPhone())
                .withClaim("name", employee.getRealname())
                .withClaim("time", new Date())
                .sign(Algorithm.HMAC256(KEY));
    }
    public static Employee decoder(String token) {
        final DecodedJWT verify = JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(token);
        Employee emp = new Employee();
        emp.setId(verify.getClaim("id").asLong());
        emp.setRealname(verify.getClaim("name").asString());
        emp.setPhone(verify.getClaim("phone").asString());
        return emp;
    }
}
